{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "64d8626f-cae5-4f6d-b83f-ea00cb51b3e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "with open('data/results_example.jsonl','r') as f:\n",
    "    data = f.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9c298fdf-f5fd-4c8b-b934-31577b546a48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"id\": \"827\", \"apis\": [{\"name\": \"product_metadata_update\", \"description\": \"更新产品的元数据，例如描述、标签等\", \"parameters\": {\"type\": \"object\", \"properties\": {\"product_id\": {\"type\": \"string\", \"description\": \"要更新的产品ID\"}, \"product_name\": {\"type\": \"string\", \"description\": \"产品名称\"}, \"product_description\": {\"type\": \"string\", \"description\": \"产品描述\"}, \"tags\": {\"type\": \"string\", \"description\": \"产品标签，用逗号分隔\"}, \"is_active\": {\"type\": \"boolean\", \"description\": \"产品是否激活\", \"default\": true}}, \"required\": [\"product_id\", \"product_name\", \"product_description\", \"is_active\"]}}, {\"name\": \"milk_quality_analysis\", \"description\": \"分析牛奶的质量，包括成分和卫生指标\", \"parameters\": {\"type\": \"object\", \"properties\": {\"fat_content\": {\"type\": \"number\", \"description\": \"牛奶中的脂肪含量（百分比）\"}, \"protein_content\": {\"type\": \"number\", \"description\": \"牛奶中的蛋白质含量（百分比）\"}, \"lactose_content\": {\"type\": \"number\", \"description\": \"牛奶中的乳糖含量（百分比）\"}, \"somatic_cell_count\": {\"type\": \"number\", \"description\": \"牛奶中的体细胞数（每毫升）\"}, \"total_bacterial_count\": {\"type\": \"number\", \"description\": \"牛奶中的总细菌数（每毫升）\"}}, \"required\": [\"fat_content\", \"protein_content\", \"lactose_content\", \"somatic_cell_count\", \"total_bacterial_count\"]}}, {\"name\": \"monitor_machine_health\", \"description\": \"监控机器设备的健康状态，减少故障和停机时间\", \"parameters\": {\"type\": \"object\", \"properties\": {\"machine_id\": {\"type\": \"string\", \"description\": \"机器设备的唯一标识符\"}, \"vibration_levels\": {\"type\": \"number\", \"description\": \"机器运作时的振动幅度\"}, \"temperature\": {\"type\": \"number\", \"description\": \"机器运作时的温度\"}, \"operating_hours\": {\"type\": \"number\", \"description\": \"机器已连续运行的小时数\"}, \"include_downtime_analysis\": {\"type\": \"boolean\", \"description\": \"是否包括停机分析报告\"}}, \"required\": [\"machine_id\", \"vibration_levels\", \"temperature\", \"operating_hours\", \"include_downtime_analysis\"]}}], \"user_messages\": [\"Answer the following question: You are considering whether to buy a product. You look at the reviews. Would the review below be a positive or negative review? This is definitely a Budweiser beer and its quality is up to the standards as before.\\\\nAnswer:\", \"我最近在关注牛奶的质量，特别是卫生指标。据我了解，牛奶中的体细胞数是200000每毫升，总细菌数是30000每毫升。\", \"另外，我还注意到牛奶中的乳糖含量是4.8%。\", \"脂肪含量方面，牛奶的脂肪含量是3.5%。\", \"蛋白质含量也很重要，牛奶中的蛋白质含量是3.2%。\", \"Alex: 你好，Sophie。你看起来很忙，能告诉我你在做什么吗？\\\\nSophie: 嗨，Alex。我正在施展一种强大的魔法，它可以为我们战斗带来巨大的优势。\\\\nAlex: 真的吗？那听起来对我们的任务很有帮助。你需要我帮忙吗？\\\\nSophie: 当然了，我们需要结合你的力量和我的魔法来打败这些妖怪。\\\\n根据以上这段对话内容回答：\\\\n两位角色的技能组合是否足够强大来对抗那些妖怪？\"], \"targets\": [[], [], [], [], {\"name\": \"milk_quality_analysis\", \"arguments\": {\"fat_content\": 3.5, \"protein_content\": 3.2, \"lactose_content\": 4.8, \"somatic_cell_count\": 200000, \"total_bacterial_count\": 30000}}, []]}\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1b221ecf-b5cf-4e70-a0aa-b5ad4b6f2c58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[],\n",
       " [],\n",
       " [{'name': 'update_inventory',\n",
       "   'arguments': {'product_id': 'P001',\n",
       "    'quantity': 50,\n",
       "    'warehouse_id': 'WH002',\n",
       "    'timestamp': '2023-04-05 14:30:00',\n",
       "    'update_reason': '供应商补货'}}]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('saves/qwen2_7b/lora/sft_1209_rank64_train0219_5epoch/results_7b_5epoch.json','r') as f:\n",
    "    predicts = json.loads(f.read())\n",
    "eval(predicts[0]['predict'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "99780724-7822-44eb-a1c6-caa00550e398",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('result_5epoch.jsonl', 'w', encoding='utf-8') as file:\n",
    "    # 遍历数据列表\n",
    "    for i in range(len(data)):\n",
    "        d = json.loads(data[i])\n",
    "        res = eval(predicts[i]['predict'])\n",
    "        d['targets'] = res\n",
    "        # 将每个字典对象转换为 JSON 字符串\n",
    "        json_line = json.dumps(d, ensure_ascii=False)\n",
    "        # 将 JSON 字符串写入文件，并添加换行符\n",
    "        file.write(json_line + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "98565ed6-39ff-43e1-a518-a6910199b01b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "57187e01-5dc8-4611-8d58-e64ae06a8676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"id\": \"827\", \"apis\": [{\"name\": \"product_metadata_update\", \"description\": \"更新产品的元数据，例如描述、标签等\", \"parameters\": {\"type\": \"object\", \"properties\": {\"product_id\": {\"type\": \"string\", \"description\": \"要更新的产品ID\"}, \"product_name\": {\"type\": \"string\", \"description\": \"产品名称\"}, \"product_description\": {\"type\": \"string\", \"description\": \"产品描述\"}, \"tags\": {\"type\": \"string\", \"description\": \"产品标签，用逗号分隔\"}, \"is_active\": {\"type\": \"boolean\", \"description\": \"产品是否激活\", \"default\": true}}, \"required\": [\"product_id\", \"product_name\", \"product_description\", \"is_active\"]}}, {\"name\": \"milk_quality_analysis\", \"description\": \"分析牛奶的质量，包括成分和卫生指标\", \"parameters\": {\"type\": \"object\", \"properties\": {\"fat_content\": {\"type\": \"number\", \"description\": \"牛奶中的脂肪含量（百分比）\"}, \"protein_content\": {\"type\": \"number\", \"description\": \"牛奶中的蛋白质含量（百分比）\"}, \"lactose_content\": {\"type\": \"number\", \"description\": \"牛奶中的乳糖含量（百分比）\"}, \"somatic_cell_count\": {\"type\": \"number\", \"description\": \"牛奶中的体细胞数（每毫升）\"}, \"total_bacterial_count\": {\"type\": \"number\", \"description\": \"牛奶中的总细菌数（每毫升）\"}}, \"required\": [\"fat_content\", \"protein_content\", \"lactose_content\", \"somatic_cell_count\", \"total_bacterial_count\"]}}, {\"name\": \"monitor_machine_health\", \"description\": \"监控机器设备的健康状态，减少故障和停机时间\", \"parameters\": {\"type\": \"object\", \"properties\": {\"machine_id\": {\"type\": \"string\", \"description\": \"机器设备的唯一标识符\"}, \"vibration_levels\": {\"type\": \"number\", \"description\": \"机器运作时的振动幅度\"}, \"temperature\": {\"type\": \"number\", \"description\": \"机器运作时的温度\"}, \"operating_hours\": {\"type\": \"number\", \"description\": \"机器已连续运行的小时数\"}, \"include_downtime_analysis\": {\"type\": \"boolean\", \"description\": \"是否包括停机分析报告\"}}, \"required\": [\"machine_id\", \"vibration_levels\", \"temperature\", \"operating_hours\", \"include_downtime_analysis\"]}}], \"user_messages\": [\"Answer the following question: You are considering whether to buy a product. You look at the reviews. Would the review below be a positive or negative review? This is definitely a Budweiser beer and its quality is up to the standards as before.\\\\nAnswer:\", \"我最近在关注牛奶的质量，特别是卫生指标。据我了解，牛奶中的体细胞数是200000每毫升，总细菌数是30000每毫升。\", \"另外，我还注意到牛奶中的乳糖含量是4.8%。\", \"脂肪含量方面，牛奶的脂肪含量是3.5%。\", \"蛋白质含量也很重要，牛奶中的蛋白质含量是3.2%。\", \"Alex: 你好，Sophie。你看起来很忙，能告诉我你在做什么吗？\\\\nSophie: 嗨，Alex。我正在施展一种强大的魔法，它可以为我们战斗带来巨大的优势。\\\\nAlex: 真的吗？那听起来对我们的任务很有帮助。你需要我帮忙吗？\\\\nSophie: 当然了，我们需要结合你的力量和我的魔法来打败这些妖怪。\\\\n根据以上这段对话内容回答：\\\\n两位角色的技能组合是否足够强大来对抗那些妖怪？\"], \"targets\": [[], [], [], [], {\"name\": \"milk_quality_analysis\", \"arguments\": {\"fat_content\": 3.5, \"protein_content\": 3.2, \"lactose_content\": 4.8, \"somatic_cell_count\": 200000, \"total_bacterial_count\": 30000}}, []]}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9a9b20b9-9a48-4313-b156-9ae8f20b0c1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '4',\n",
       " 'apis': [{'name': 'inspect_materials',\n",
       "   'description': '检查建设材料的质量',\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'material_id': {'type': 'string', 'description': '材料的唯一标识'},\n",
       "     'sample_size': {'type': 'number', 'description': '用于检查的样本数量'},\n",
       "     'inspection_standard': {'type': 'string', 'description': '质量检查所依据的标准'},\n",
       "     'result': {'type': 'string', 'description': '此次检查的结果'},\n",
       "     'inspector_id': {'type': 'string', 'description': '负责检查的人员ID'},\n",
       "     'date': {'type': 'string', 'description': '检查的日期(时间格式为YYYY-MM-DD)'}},\n",
       "    'required': ['material_id',\n",
       "     'sample_size',\n",
       "     'inspection_standard',\n",
       "     'result',\n",
       "     'inspector_id',\n",
       "     'date']}},\n",
       "  {'name': 'analyze_hashtags',\n",
       "   'description': '分析指定微博或帖子中的标签',\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'hashtags': {'type': 'string',\n",
       "      'description': '要分析的标签列表，用逗号分隔'},\n",
       "     'start_date': {'type': 'string',\n",
       "      'description': '分析的开始日期(时间格式为YYYY-MM-DD)'},\n",
       "     'end_date': {'type': 'string', 'description': '分析的结束日期(时间格式为YYYY-MM-DD)'},\n",
       "     'platform': {'type': 'string', 'description': '分析的平台，如Twitter或Instagram'},\n",
       "     'include_sentiment': {'type': 'boolean', 'description': '是否包含情感分析结果'}},\n",
       "    'required': ['hashtags', 'start_date', 'end_date', 'platform']}}],\n",
       " 'user_messages': ['列出ASP.NET中所有葫节点和架构更新。重点关注从ASP.NET 4发展到ASP.NET Core的重大变化。并用简要但是明确的方式阐述 ASP.NET Core 和ASP.NET Framework之间的核心区别。',\n",
       "  '最近我对环保、气候变化和绿色地球这些话题特别感兴趣，想了解一下在Twitter上这些话题的讨论情况。从2022年1月1日到年底，也就是12月31日，这段时间内，人们是怎么看待这些问题的呢？如果可以的话，我还想看看这些讨论的情感倾向是怎样的。',\n",
       "  '开发一个Ruby程序，该程序读取一个文本文件，并统计文件中每个单词出现的次数。'],\n",
       " 'predict': \"[[], [{'name': 'analyze_hashtags', 'arguments': {'hashtags': '环保,气候变化,绿色地球','start_date': '2022-01-01', 'end_date': '2022-12-31', 'platform': 'Twitter', 'include_sentiment': True}}], []]\"}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predicts[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df822071-9046-4eb7-9e1b-f703fa2abc29",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
